<?php
/**
 * User: lsx
 * Date: 2017/6/3
 * Time: 15:18
 * Progress:0%
 */

namespace app\addons\controller;


use library\common\web\ImageUploadHandler;
use library\common\web\UploadException;
use think\Controller;
use think\Db;

class WebUploader extends Controller
{
    public function index(){
        return view();
    }

    public function upload(){
        $ids=input('id');
        $parent_ele=input('parent_ele');
        $this->assign('ids',$ids);
        $this->assign('parent_ele',$parent_ele);
        return view();
    }

    public function simple(){
        $ids=input('id');
        $parent_ele=input('parent_ele');
        $this->assign('ids',$ids);
        $this->assign('parent_ele',$parent_ele);
        return view();
    }

    public function multiple(){
        $ids=input('id');
        $parent_ele=input('parent_ele');
        $this->assign('ids',$ids);
        $this->assign('parent_ele',$parent_ele);
        return view();
    }

    public function images_list(){
        $id=input('id');
        $id_arr=explode(',',$id);

        $data=Db::name('file')->where('id','in',$id_arr)->select();
        return $data;
    }

    public function uploadImg()
    {
        $path=array(
            'STORAGE_DIR' => __STATIC__.'/upload',
            'BASE_URL'    => __STATIC__.'/upload/',
        );
        try {
            $h = new ImageUploadHandler(
                array(
                    'directory'   => $path['STORAGE_DIR'],
                    'maxFileSize' => 5 * 1024 * 1024
                )
            );
            $h->setThumbnail('thumb', null, '_thumb', 100, 100, 0);
            $res = $h->save();
            $fileInfo = array(
                'time'   => time(),
                'type'=>$res['type'],
                'size'         => $res['size'],
                'extension'    => $res['extension'],
                'filename'     => $res['path'],
                'source_name' => $res['raw_name']
            );
            $fileId = Db::name('file')->insertGetId($fileInfo);

            $data = array(
                'file_id'  => $fileId,
                'base_url' => $path['BASE_URL'],
                'url'      => $res['path'],
                'thumb'    => $res['thumbnails']['thumb']['path']
            );
//            return json_encode(array(
//                'success'=>true,
//                'message'=>'upload success.',
//                'data'=>$data
//            ),true);
            $this->jsonSuccess('upload success.', '',$data);
        } catch (UploadException $exception) {
            return json_encode(array(
                'success'=>false,
                'message'=>$exception->getMessage(),
                'code'=>$exception->getCode()
            ),true);
//            $this->jsonError($exception->getMessage(), $exception->getCode());
        }
    }

    public function delete_image(){
        $id=input('id');
        $res=Db::name('file')->where(['id'=>$id])->delete();
        if($res){
            return array(
                'success'=>true,
            );
        }else{
            return array(
                'success'=>false
            );
        }
    }

    protected function jsonSuccess($message=null,$url=null,$data=null){
        $response=array(
            'success'=>true,
            'message'=>$message,
            'url'=>$url
        );
        if($data !== null){
            $response['data']=$data;
        }
        $this->jsonResult($response);
    }
    protected function jsonResult($data,$options=0){
        header(('Content-Type:application/json;chartset=utf-8'));
        exit(json_encode($data,$options));
    }

}